#include <stdio.h>
#include "typedef.h"
#include "os_sys.h"
#include "os_tick.h"
#include "eeprom_driver.h"
#include "eeprom_bsp.h"





struct EEPROM_TASK_S
{
	SysTick_t task_tick;
	EEPORM_DATA data;
};

static struct EEPROM_TASK_S eeprom_task;

u8 eeprom_flag=0;
u32 test_data[4]={0,24000,0x806F000};



void eeprom_main(void)
{
	if (0 == systick_get_difference_assign(&eeprom_task.task_tick, Tnms(1000)))
        return;
	switch(eeprom_flag)
	{
		case 0x01:
			eeprom_task.data.D.test1 ++;
		break;
		case 0x02:
			eeprom_task.data.D.test7 += 1.56;
		break;
		case 0x03:
			eeprom_task.data.D.test4 += 3;
		break;
		case 0x20:
			eeporm_bsp_save_data();
		break;
	}
	eeprom_flag=0;

}
static void eeprom_main_run(void)
{
	switch(sys_task_state_get(TASKID_EEPROM ))
	{
		case TASK_STAT_FREE:
		case TASK_STAT_POWER_ON:
		case TASK_STAT_INITIAL:
			sys_task_state_set(TASKID_EEPROM,TASK_STAT_RUN);
		break;
		case TASK_STAT_RUN:
            eeprom_main();
		break;
		case TASK_STAT_UNINITIAL:
			sys_task_state_set(TASKID_EEPROM,TASK_STAT_POWER_OFF);
		break;
		case TASK_STAT_POWER_OFF:
		case TASK_STAT_SLEEP:

		break;
		default:
			sys_task_state_set(TASKID_EEPROM,TASK_STAT_POWER_OFF);
		break;
	}

}
static void eeprom_peripheral_sleep(void)
{}


static void eeprom_message_process(E_DATAID DatID, const u32 data)
{
	switch(DatID)
	{
		case DATID_EEPROM_TEST1:
			*(u32*)data=eeprom_task.data.D.test1;
		break;
		case DATID_EEPROM_TEST7:
			*(float*)data=eeprom_task.data.D.test7;
		break;
		case DATID_EEPROM_TEST1_4_6_7_READ:
			MSG_R(eeprom_task.data.D.test1,eeprom_task.data.D.test4,eeprom_task.data.D.test6,eeprom_task.data.D.test7);
		break;
		case DATID_EEPROM_TEST1_4_6_7_WRITE:
			MSG_P(eeprom_task.data.D.test1,eeprom_task.data.D.test4,eeprom_task.data.D.test6,eeprom_task.data.D.test7);
		break;
		case DATID_EEPROM_SAVE:
			eeporm_bsp_save_data();
		break;
		default:break;
	}
}
//必须设置数据默认值
void eeprom_data_default_init(void)
{
	eeprom_page_buff_pading(eeprom_task.data.buff);//填充数据
	eeprom_task.data.D.test1 = 1200;
	eeprom_task.data.D.test2 = 240000;
	eeprom_task.data.D.test3 = 100;
	snprintf(eeprom_task.data.D.string,16,"liu huo");
	eeprom_task.data.D.test4 = -1234;
	eeprom_task.data.D.test5 = 4325;
	eeprom_task.data.D.test6 = 'O';
	eeprom_task.data.D.test7 = 123.456;
	eeprom_data_crc_set(&eeprom_task.data);
}
static void eeprom_variable_init(void)
{
    eeprom_data_default_init();
    eeprom_bsp_init(&eeprom_task.data);
}
//CONSTRUCTOR_DEFAUTL()
void eeprom_task_create(void)
{
	struct task_s task = TASK_DEFAULT_INIT();

	task.ID 		= TASKID_EEPROM;
	task.state		= TASK_STAT_FREE;
	task.TaskTime	= 0;
	task.run 		= eeprom_main_run;
	task.sleep_init = eeprom_peripheral_sleep;
	task.msg_process= eeprom_message_process;
	task.task_tick 	= NULL;
	task.is_used 	= 1;
	task.task_init 	= eeprom_variable_init;
	sys_task_add(&task);
}

